package com.atech.ebayadmin.system.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.atech.ebayadmin.common.pager.Pager;
import com.atech.ebayadmin.common.pager.PagerHelper;
import com.atech.ebayadmin.common.struts.BaseAction;
import com.atech.ebayadmin.merchandise.service.IMerchandiseService;
import com.atech.ebayadmin.system.model.Role;
import com.atech.ebayadmin.system.model.User;
import com.atech.ebayadmin.system.service.ISystemService;
import com.atech.ebayadmin.system.vo.UserVO;
//Added by Doug to remove session when logging out - Oct18
import com.opensymphony.xwork2.ActionContext;

public class SystemAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = -5473018305485591227L;
	private static final Log log = LogFactory.getLog(SystemAction.class);
	private ISystemService systemService;
	private IMerchandiseService mdService;
	public String userName;
	public String password;
	public String roleId;
	public String message;
	public User userPO;
	public Integer userId;
	public Integer ebayActId;
	public List<User> userList = null;
	

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public ISystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(ISystemService systemService) {
		this.systemService = systemService;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String userLogin() throws Exception {
		try{
			UserVO user = systemService.login(userName, password);
			super.getSession().put("User", user);
			this.setUserName(null);
			this.setPassword(null);
		}catch (Exception e) {
			e.printStackTrace();
			this.setMessage(e.getMessage());
			return ERROR;
		}
        return SUCCESS;
    }
	public String loginOut() throws Exception {
		try{
			super.getSession().remove("User");
			this.setMessage(null);
			
			//Added by Doug to remove session when logging out - Oct18
			ActionContext.getContext().getSession().clear();			
		}catch (Exception e) {
			e.printStackTrace();
			this.setMessage(e.getMessage());
			return ERROR;
		}
        return SUCCESS;
    }
	public String changePassword() throws Exception {
		try{
			systemService.updatePassword(userPO);
			this.setMessage("Operation is success!");
		}catch (Exception e) {
			this.setMessage(e.getMessage());
		}
        return SUCCESS;
    }
	public String listUser() throws Exception {
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			Pager pager = PagerHelper.getPager(request);
			List<User> userList = systemService.listUser(userName, pager);
			this.setUserList(userList);
			request.setAttribute("pager", pager);
	        return SUCCESS;
		}catch (Exception e) {
			ServletActionContext.getRequest().setAttribute("message", e.getMessage());
			return super.handleException(e);
		}
    }
	public String toAddUser() throws Exception {
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			List<Role> roleList = systemService.listAllRole();
			request.setAttribute("roleList", roleList);
	        return SUCCESS;
		}catch (Exception e) {
			ServletActionContext.getRequest().setAttribute("message", e.getMessage());
			return super.handleException(e);
		}
    }
	public String addUser() throws Exception {
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			Integer userId = systemService.saveUser(userPO);
			request.setAttribute("id","userId");
			request.setAttribute("idValue", userId);
			request.setAttribute("actionName", "viewUser");
	        return SUCCESS;
		}catch (Exception e) {
			ServletActionContext.getRequest().setAttribute("message", e.getMessage());
			return super.handleException(e);
		}
    }
	public String viewUser() throws Exception {
		try{
			User userPO = systemService.getUser(userId);
			this.setUserPO(userPO);
	        return SUCCESS;
		}catch (Exception e) {
			ServletActionContext.getRequest().setAttribute("message", e.getMessage());
			return super.handleException(e);
		}
    }
	public String deleteUser() throws Exception {
		try{
			systemService.deleteUser(userId);
			ServletActionContext.getRequest().setAttribute("message", "已成功删除用户!");
	        return SUCCESS;
		}catch (Exception e) {
			ServletActionContext.getRequest().setAttribute("message", e.getMessage());
			return super.handleException(e);
		}
    }
	public String toEditUser() throws Exception {
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			User userPO = systemService.getUser(userId);
			this.setUserPO(userPO);
			List<Role> roleList = systemService.listAllRole();
			StringBuilder sb = new StringBuilder();
			request.setAttribute("choosedAct", sb.toString());
			request.setAttribute("roleList", roleList);
	        return SUCCESS;
		}catch (Exception e) {
			ServletActionContext.getRequest().setAttribute("message", e.getMessage());
			return super.handleException(e);
		}
    }
	public String editUser() throws Exception {
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			systemService.updateUser(userPO);
			request.setAttribute("id","userId");//跳转到中间页面再实现查询
			request.setAttribute("idValue", userPO.getUserId());
			request.setAttribute("actionName", "viewUser");
	        return SUCCESS;
		}catch (Exception e) {
			ServletActionContext.getRequest().setAttribute("message", e.getMessage());
			return super.handleException(e);
		}
    }
	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}



	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public User getUserPO() {
		return userPO;
	}

	public void setUserPO(User userPO) {
		this.userPO = userPO;
	}

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public Integer getEbayActId() {
		return ebayActId;
	}

	public void setEbayActId(Integer ebayActId) {
		this.ebayActId = ebayActId;
	}

	public IMerchandiseService getMdService() {
		return mdService;
	}

	public void setMdService(IMerchandiseService mdService) {
		this.mdService = mdService;
	}
}
